/*
$(function (){
    $(".jieSuan").click(function (){
        var shuzu=new Array();
        var idshuzu=new Array();
        shuzu=$(".select-item")
        for (var i=0;i<shuzu.length;i++){
            if (shuzu[i].checked){
                var id=shuzu[i].value;
                idshuzu.push(id);
            }
        }

        $.ajax({
            url: "addOrderServlet",
            data:{"id":idshuzu},
            dataType: "text",
            type: "post",
            traditional:true,
            success: function (id) {
                if (id==0){
                    alert("失败")
                }else {
                }
            }
        })
    })



})
*/

document.getElementById('checkout').addEventListener('click', function () {
    const selectedItems = document.querySelectorAll('.select-item:checked');
    const orderDetails = [];

    selectedItems.forEach(item => {
        const itemId = item.value;
        const itemRow = item.closest('ul');

        const img = itemRow.querySelector('li:nth-child(2)').src;
        const name = itemRow.querySelector('li:nth-child(3)').innerText;
        const price = parseFloat(itemRow.querySelector('.price').innerText.replace('¥', ''));
        const amount = parseInt(itemRow.querySelector('input[name="amount"]').value);
        const totalPrice = price * amount;

        orderDetails.push({img, name, price, amount, totalPrice});
    });

    if (orderDetails.length > 0) {
        // 将订单详细信息存储到会话中
        fetch('StoreOrderDetailsServlet', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(orderDetails)
        }).then(response => {
            if (response.ok) {
                window.location.href = 'sureOrder.html';
            }
        });
    } else {
        alert('请先选择商品');
    }
});
